<template>
  <div class="xtx-goods-page" v-if="goodDetai.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodDetai.categories[1].id}`">{{
          goodDetai.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goodDetai.categories[0].id}`">{{
          goodDetai.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goodDetai.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 新增结构 -->
        <!-- 图片预览区 -->
        <div class="media">
          <GoodsImage :pics="goodDetai.mainPictures" />
          <!-- 商品售卖信息 -->
          <GoodsSales :goodDetai="goodDetai" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 商品基本信息 -->
          <GoodsName :goodDetai="goodDetai" />
          <!-- 商品spu（规格）选择组件 -->
          <GoodsSku :goods="goodDetai" @change="getSku" />
          <!-- 商品数量加减 -->
          <!--
            v-model 写到自定义组件身上yourname、
            1.简化写法
            2.修改值 => 还是子传父
           -->
          <XtxNumbox v-model="buyNum" :max="goodDetai.inventory" />
          <!-- <XtxButton type="gray" size="small" /> -->
          <XtxButton style="margin-top: 20px">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <!--  -->
            <GoodsTabs />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods.js'
import { ref, onMounted, provide } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsTabs from './components/goods-tabs'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsImage, GoodsSales, GoodsName, GoodsTabs },
  setup () {
    // 商品详情
    const goodDetai = ref({})
    provide('goodDetai', goodDetai)
    // 商品购买梳理
    const buyNum = ref(1)
    const route = useRoute()
    const getDoodDetail = async () => {
      const { result: res } = await findGoods(route.params.id)
      console.log('商品详情', res)
      goodDetai.value = res
    }
    // 获取当前选中的stu数据
    /**
     * 有效sku包含两个信息
     * 1.库存
     * 2.价格
     */
    const getSku = (sku) => {
      console.log(sku)
      if (sku.skuId) {
        goodDetai.value.price = sku.price
        goodDetai.value.oldPrice = sku.oldPrice
        goodDetai.value.inventory = sku.inventory
      }
    }
    onMounted(() => {
      getDoodDetail()
    })
    return { goodDetai, getSku, buyNum }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
